package com.storebits.testsystem.views;

import com.storebits.testsystem.Car;
import com.storebits.testsystem.views.util.JsfUtil;
import com.storebits.testsystem.views.util.PaginationHelper;
import com.storebits.testsystem.beans.CarFacade;

import java.io.Serializable;
import java.util.List;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;

@ManagedBean(name = "carController")
@SessionScoped
public class CarController implements Serializable {

    private Car current;
    @EJB
    private CarFacade ejbFacade;
    private List<Car> items;

    public Car getSelected() {
        if (current == null) {
            current = new Car();
        }
        return current;
    }

    private CarFacade getFacade() {
        return ejbFacade;
    }


    public String prepareList() {
        //items = getFacade().findAll();
        return "List";
    }

    public String prepareView(Car selectedCar) {
        current = selectedCar;
        return "View";
    }

    public String prepareCreate() {
        current = new Car();
        return "Create";
    }

    public String create() {
        try {
            getFacade().create(current);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("CarCreated"));
            items = getFacade().findAll();
            return prepareCreate();
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

    public String prepareEdit(Car car) {
        current = car;
        return "Edit";
    }

    public String update() {
        try {
            getFacade().edit(current);
            items = getFacade().findAll();
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("CarUpdated"));
            return "View";
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

    public String destroy(Car car) {
        current = car;
        performDestroy();
        return "List";
    }

    public String destroyAndView() {
        performDestroy();
        if (items.size() >= 0) {
            return "View";
        } else {
            // all items were removed - go back to list
            return "List";
        }
    }

    private void performDestroy() {
        try {
            getFacade().remove(current);
            items = getFacade().findAll();
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("CarDeleted"));
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
        }
    }

    public List<Car> getItems() {
        if(items == null) {
            items = ejbFacade.findAll();
        }
        System.out.println("Items:" + items);
        return items;
    }
    
    public SelectItem[] getItemsAvailableSelectMany() {
        return JsfUtil.getSelectItems(ejbFacade.findAll(), false);
    }

    public SelectItem[] getItemsAvailableSelectOne() {
        return JsfUtil.getSelectItems(ejbFacade.findAll(), true);
    }

    @FacesConverter(forClass = Car.class)
    public static class CarControllerConverter implements Converter {

        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
}
            CarController controller = (CarController) facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "carController");
            return controller.ejbFacade.find(getKey(value));
        }

        java.lang.String getKey(String value) {
            java.lang.String key;
            key = value;
            return key;
        }

        String getStringKey(java.lang.String value) {
            StringBuffer sb = new StringBuffer();
            sb.append(value);
            return sb.toString();
        }

        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof Car) {
                Car o = (Car) object;
                return getStringKey(o.getRegistrationNumber());
            } else {
                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + CarController.class.getName());
            }
        }
    }
}
